//
//  BSPersonStiscPopView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/5/24.
//

import Foundation
import UIKit
import JFPopup

protocol BSPersonStiscPopViewDelegate: AnyObject {
    func handleSelected(_ dept: BalzByDeptWdModel?)
}

class BSPersonStiscPopView: UIView {
    let headerV = BSSearchButton(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 60))
    let tableView = UITableView(frame: .zero, style: .grouped)

    var resultV: BSPersonStiscResultView!

    weak var delegate: BSPersonStiscPopViewDelegate?

    var users: [BalzByDeptWdModel?] = []
        
    init(withUsers users: [BalzByDeptWdModel?]) {
        super.init(frame: .zero)
        resultV = BSPersonStiscResultView(withUsers: users)
        setupUI()
        self.users = users
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func showSearchTapped() {
        if resultV.superview == nil {
            resultV.delegate = self
            addSubview(resultV)
            resultV.snp.makeConstraints { make in
                make.top.left.right.bottom.equalToSuperview()
            }
            resultV.resetUI()
        }
    }
    
    @objc func cencelTFTapped() {
        if resultV.superview != nil {
            resultV.delegate = nil
            resultV.removeFromSuperview()
        }
    }
    
    func setupUI() {
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .white
        addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(10)
            make.bottom.equalTo(self.safeAreaLayoutGuide.snp.bottom)
        }
        
        headerV.titleL.text = "搜索保安"
        headerV.padding = 25
        headerV.addTarget(self, action: #selector(showSearchTapped), for: .touchUpInside)
        tableView.tableHeaderView = headerV
        
        resultV.headerV.cancelBtn.addTarget(self, action: #selector(cencelTFTapped), for: .touchUpInside)
    }
}

extension BSPersonStiscPopView: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return users.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = users[indexPath.row]
        let ID : String = "BSDeptStiscPopCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: ID)
        if cell == nil { cell = UITableViewCell(style: .default, reuseIdentifier: ID) }
        cell?.textLabel?.text = data?.lzrmc
        cell?.textLabel?.font = .systemFont(ofSize: 15)
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let modal = users[indexPath.row]
        delegate?.handleSelected(modal)
    }
}

extension BSPersonStiscPopView: BSPersonStiscResultViewDelegate {
    func handleDeptSearchDidSelected(_ modal: BalzByDeptWdModel) {
        delegate?.handleSelected(modal)
    }
}
